package mumu.android.lib.view;

import java.util.ArrayList;
import java.util.List;

import mumu.android.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TreeListView<T> extends ListView {
	//总数据
	List<TreeEntity<T>> list = new ArrayList<TreeEntity<T>>();
	List<TreeEntity<T>> templist = new ArrayList<TreeEntity<T>>();
	Context mContext;
	private ITreeInterface<T> mITreeInterface;

	public TreeListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
	}

	public TreeListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	public TreeListView(Context context) {
		super(context);
		mContext = context;
	}

	private TreeViewAdapter treeViewAdapter = null;

	public void onCreate(List<TreeEntity<T>> list, ITreeInterface<T> mITreeInterface) {
		this.list = list;
		this.mITreeInterface = mITreeInterface;
		//取出一级节点，默认展示一级节点
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).ParentId == 0)
				templist.add(list.get(i));
		}
		treeViewAdapter = new TreeViewAdapter(mContext, R.layout.outline);
		setAdapter(treeViewAdapter);
		setOnItemClickListener(itemClickListener);
	}

	private class TreeViewAdapter extends ArrayAdapter {

		public TreeViewAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
		}

		public int getCount() {
			return templist.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (mITreeInterface != null)
				convertView = mITreeInterface.getView(templist.get(position), position, convertView);
			return convertView;
		}
	}

	public interface ITreeInterface<T> {
		View getView(TreeEntity<T> t, int position, View convertView);

		void onItemClick(TreeEntity<T> t, int position);
	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
			if (!templist.get(position).HasChild) {
				if (mITreeInterface != null)
					mITreeInterface.onItemClick(templist.get(position), position);
				return;
			}
			//收起子节点
			if (templist.get(position).IsExpanded) {
				templist.get(position).IsExpanded = false;
				TreeEntity<T> OutlineElement = templist.get(position);
				ArrayList<TreeEntity<T>> temp = new ArrayList<TreeEntity<T>>();
				//删除子节点
				for (int i = position + 1; i < templist.size(); i++) {
					if (OutlineElement.Level >= templist.get(i).Level) {
						break;
					}
					temp.add(templist.get(i));
				}
				templist.removeAll(temp);
				//通知数据更新
				treeViewAdapter.notifyDataSetChanged();

			} else {
				//展开子节点
				templist.get(position).IsExpanded = true;
				int level = templist.get(position).Level;
				int nextLevel = level + 1;

				for (TreeEntity<T> chenEn : list) {
					int j = 1;
					if (chenEn.ParentId == templist.get(position).Id) {
						chenEn.Level = nextLevel;
						chenEn.IsExpanded = false;
						templist.add(position + j, chenEn);
						j++;
					}
				}
				//通知数据更新
				treeViewAdapter.notifyDataSetChanged();
			}
		}
	};

}
